package com.woniu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.model.AllRoomnum;
import com.woniu.model.Message;
import com.woniu.model.Result;
import com.woniu.service.AllRoomnumService;
import com.woniu.service.MessageService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jas
 * @since 2024年06月05日
 */
@RestController
@RequestMapping("/message")
@Api(tags = "消息接口")
public class MessageController {
    @Resource
    private MessageService messageService;
    @Resource
    private AllRoomnumService allRoomnumService;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 查看我的消息
     * @param token
     * @return
     *
     * @RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum,
     *                                                    @RequestParam(value = "pageSize",required = true,defaultValue = "10") Integer pageSize,
     */
    @GetMapping("/loadMyChatMessage")
    @ApiOperation("查询我的聊天数据")
    public Result<List<Message>> loadMyChatMessage(
                                                   @RequestHeader String token
                                                   ) throws Exception {
        String refreshToken = redisUtil.get(token);
        int accountId = Integer.parseInt(JwtUtil.parseToken(refreshToken,JwtUtil.userSecret).get("accountId").toString());
        int userId = Integer.parseInt(JwtUtil.parseToken(refreshToken,JwtUtil.userSecret).get("id").toString());

        AllRoomnum allRoomnum = allRoomnumService.getOne(new QueryWrapper<AllRoomnum>().eq("user_id", userId));
        if (allRoomnum==null) {
            throw new Exception("还未聊天");
        }
        List<Message> list = messageService.list(new QueryWrapper<Message>().eq("room_num", allRoomnum.getRoomNum()).eq("account_id", accountId));
        if (list==null||list.size()==0) {
            throw new Exception("没有聊天数据");
        }
        return Result.newListInstance(Message.class).setData(list);
    }
}

